package com.edu.app.util.http;

import com.edu.app.exception.wechat.NotSupportHttpImpl;
import com.edu.app.util.http.impl.OkHttpInterface;
import com.edu.app.util.http.impl.ProxyInfo;

import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

public class HttpUtils {
    static HttpInterface httpInterface = null;
    public static  HttpInterface GetHttpInterface(HttpImplConfig config) throws NotSupportHttpImpl {
        if(httpInterface == null) {
            synchronized (HttpUtils.class) {
                if (httpInterface == null) {
                    if (config.getImpl().equalsIgnoreCase("okhttp")) {
                        Map<String, Object> map = new HashMap<>();
                        if (StringUtils.isNotEmpty(config.getHost()) &&
                                StringUtils.isNotEmpty(config.getPort()) ) {
                            int port = Integer.valueOf(config.getPort()).intValue();
                            ProxyInfo proxyInfo = new ProxyInfo(ProxyInfo.ProxyType.HTTP,
                                    config.getHost(),
                                    port,
                                    config.getUsername(),
                                    config.getUsername()
                            );
                            map.put("httpproxy", proxyInfo);
                        }
                        httpInterface = new OkHttpInterface(map);
                    } else {
                        throw new NotSupportHttpImpl("Not support http implementation: " + config.getImpl());
                    }
                }

            }
        }
        return httpInterface;
    }

}
